package com.xiaojianone.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaojianone.pojo.User;
import com.xiaojianone.service.UserService;
import com.xiaojianone.mapper.UserMapper;
import com.xiaojianone.utils.JwtHelper;
import com.xiaojianone.utils.MD5Util;
import com.xiaojianone.utils.Result;
import com.xiaojianone.utils.ResultCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
* @author 徐国健
* @description 针对表【news_user】的数据库操作Service实现
* @createDate 2025-02-18 14:18:47
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{

    @Autowired
    private UserMapper mapper;

    @Autowired
    private JwtHelper jwtHelper;

    /**
     * 登陆业务
     * @param user
     * @return
     */
    @Override
    public Result login(User user) {

        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        lambdaQueryWrapper.eq(User::getUsername,user.getUsername());

        //获取数据库中 该用户名 的信息
        User loginUser = mapper.selectOne(lambdaQueryWrapper);

        //对比用户名
        if (loginUser == null) {

            return Result.build(null, ResultCodeEnum.USERNAME_ERROR);
        }

        //对比密码
        if (!StringUtils.isEmpty(user.getUserPwd())
                && MD5Util.encrypt(user.getUserPwd()).equals(loginUser.getUserPwd())){

            //登陆成功

            String token = jwtHelper.createToken(Long.valueOf(loginUser.getUid()));
            Map data = new HashMap();
            data.put("token",token);

            return Result.ok(data);
        }

        //密码错误
        return Result.build(null,ResultCodeEnum.PASSWORD_ERROR);
    }


    /**
     *根据token获取用户数据
     * @param token
     * @return Result
     */
    @Override
    public Result getUserInfo(String token) {

        //判断token是否失效
        boolean expiration = jwtHelper.isExpiration(token);

        if (expiration) {
            //token失效 当作未登录看待
            return Result.build(null,ResultCodeEnum.NOTLOGIN);
        }

        //token未失效
        int userId = jwtHelper.getUserId(token).intValue();

        User user = mapper.selectById(userId);
        user.setUserPwd("");

        Map data = new HashMap();
        data.put("loginUser",user);

        return Result.ok(data);
    }


    /**
     * 注册用户名检查
     * @param username
     * @return
     */
    @Override
    public Result checkUserName(String username) {

        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(User::getUsername,username);

        Long count = mapper.selectCount(queryWrapper);

        if (count==0) {
            //响应成功
            return Result.ok(null);
        }

        return Result.build(null,ResultCodeEnum.USERNAME_USED);
    }


    /**
     * 注册业务
     * @param user
     * @return
     */
    @Override
    public Result regist(User user) {

        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(User::getUsername,user.getUsername());

        Long count = mapper.selectCount(queryWrapper);

        if (count > 0) {
            //该用户名已经被注册
            return Result.build(null,ResultCodeEnum.USERNAME_USED);
        }

        user.setUserPwd(MD5Util.encrypt(user.getUserPwd()));
        mapper.insert(user);

        return Result.ok(null);

    }
}




